<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/12/1 18:53
 */

namespace App\Project\Search\Decorator;

use App\Project\Search\Decorator;
use App\Project\Search\Raw;

class DiagnoseDecorator extends Decorator
{
    protected $table = 'st_medical_diagnose';

    protected $prefix = 'diagnose';

    public function getBuilder()
    {
        return $this->fillBuilder([
            $this->table,
            'st_medical_records.id',
            $this->table . '.record_id'
        ]);
    }

    public function getAccessField()
    {
        return [
            'd' => 'diagnose'
        ];
    }

    public function getAccessOperator()
    {
        return [
            'd' => ['in']
        ];
    }

    protected function condition($condition, $key)
    {
        if ($this->canAddSelect($condition)) {
            $sql = \DB::raw('(select group_concat(name) from st_disease_dictionary WHERE id IN (' . implode(',',
                    $condition['value']) . '))');

            $this->db->addSelect(\DB::raw($sql . ' as \'' . $this->prefix . '-' . $key . '\''));
        }

        return Raw::findInSet($this->table . '.diagnose', $condition['value']);
    }
}